package com.jhws.business.car.service;

import com.jhws.business.car.bo.CarConstant;
import com.jhws.business.car.bo.CarInfo;
import com.jhws.common.Page;
import com.jhws.core.db.BaseDao;
import com.jhws.util.StringUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("Duplicates")
@Service
public class CarInfoService {
    @Resource
    private BaseDao BaseDao;

    /**
     * 保存车辆信息
     *
     * @param carInfo 车辆信息
     */
    public void saveCarInfo(CarInfo carInfo) {
        carInfo.setUpdateTime(new Date());
        BaseDao.saveOrUpdate(carInfo);
    }

    /**
     * 查询车辆信息
     *
     * @param param Map
     * @return 查询车辆信息列表
     */
    @SuppressWarnings("unchecked")
    public Map<String, Object> queryCarInfoList(Map param) {
        boolean isNotPage = MapUtils.getBooleanValue(param, "isNotPage");
        String sql = "SELECT * " + " FROM car_info  WHERE 1=1 ";

        String parkId = MapUtils.getString(param, "parkId", "");
        if (StringUtils.isNotEmpty(parkId)) {
            sql += " and park_id = '" + parkId + "' ";
        }

        String carNum = MapUtils.getString(param, "plateNumber", "");
        if (StringUtils.isNotEmpty(carNum)) {
            sql += " and plate_number LIKE '%" + carNum + "%' ";
        }
        sql += " order by update_time desc";
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> list;
        if (isNotPage) {
            list = BaseDao.j().queryForList(sql);
        } else {
            Page p = BaseDao.queryPage(sql, Map.class);
            list = p.getList();
            result.put("size", p.getTotalNumber());
        }
        for (Map<String, Object> map : list) {
            int carType = MapUtils.getIntValue(map, "car_type");
            map.put("car_type", CarConstant.carType(carType));
            int cardType = MapUtils.getIntValue(map, "card_type");
            map.put("card_type", CarConstant.cardType(cardType));
        }
        result.put("list", list);
        result.put("ret_code", 1);
        return result;
    }

    /**
     * 条件查询类型
     *
     * @param where 条件
     * @return
     */
    public List<CarInfo> queryCarInfoByWhere(String where) {
        return BaseDao.findList("from CarInfo " + where);
    }

    /**
     * 删除车辆信息
     *
     * @param carInfo 车辆信息
     */
    public void deleteCarInfo(CarInfo carInfo) {
        BaseDao.h().delete(carInfo);
    }
}
